package ejb.cbm.operatoria.session;

import java.util.List;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ejb.cbm.operatoria.entity.TarjetaEntity;
import ejb.cbm.operatoria.local.TarjetaSessionLocal;

////@Stateless
public class TarjetaSession extends HibernateDaoSupport implements TarjetaSessionLocal {
	
	static Logger logger = Logger.getLogger(TarjetaSession.class.getName());
	//property constants
	public static final String BINTARJETA = "bintarjeta";
	public static final String NUMEROTARJETA = "numerotarjeta";
	public static final String TIPOTARJETA = "tipotarjeta";
	public static final String DESCRIPCIONTARJETA = "descripciontarjeta";
	public static final String ENTIDADCONTRATO = "entidadcontrato";
	public static final String OFICINACONTRATO = "oficinacontrato";
	public static final String TIPOCONTRATO = "tipocontrato";
	public static final String NUMEROCONTRATO = "numerocontrato";
	public static final String MONTOLIMITE = "montolimite";
	public static final String MONTODISPUESTO = "montodispuesto";
	public static final String NUMERODOCUMENTO = "numerodocumento";
	public static final String INDICADORSITUACION = "indicadorsituacion";
	public static final String USUARIOREGISTRO = "usuarioregistro";
	public static final String USUARIOMODIFICACION = "usuariomodificacion";

    //@PersistenceContext 
    //private EntityManager entityManager;
	
	@Autowired
	public TarjetaSession(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
    public void save(TarjetaEntity transientInstance) {
    	logger.log("saving TarjetaEntity instance", Level.INFO, null,null);
        try {
            getSession().persist(transientInstance);
            logger.log("save successful", Level.INFO, null,null);
        } catch (RuntimeException re) {
        	logger.log("save failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public void delete(TarjetaEntity persistentInstance) {
    	logger.log("deleting TarjetaEntity instance", Level.INFO, null,null);
        try {
            getSession().delete(persistentInstance);
            logger.log("delete successful", Level.INFO, null,null);
        } catch (RuntimeException re) {
        	logger.log("delete failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public TarjetaEntity update(TarjetaEntity detachedInstance) {
    	logger.log("updating TarjetaEntity instance", Level.INFO, null,null);
        try {
            TarjetaEntity result = (TarjetaEntity) getSession().merge(detachedInstance);
            logger.log("update successful", Level.INFO, null,null);
            return result;
        } catch (RuntimeException re) {
        	logger.log("update failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public TarjetaEntity findById( Long id) {
    	logger.log("finding TarjetaEntity instance with id: " + id, Level.INFO, null,null);
        try {
            TarjetaEntity instance = (TarjetaEntity) getSession().get(TarjetaEntity.class, id);
            return instance;
        } catch (RuntimeException re) {
        	logger.log("find failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    //@SuppressWarnings("unchecked")
    public List<TarjetaEntity> findByProperty(String propertyName, Object value) {
    	logger.log("finding TarjetaEntity instance with property: " + propertyName
    						+ ", value: " + value, Level.INFO, null,null);
		try {
			String queryString = "select model from TarjetaEntity model where model." 
			 						+ propertyName + "= :propertyValue";
			return getSession().createQuery(queryString)
 				.setParameter("propertyValue", value).list();
		} catch (RuntimeException re) {
			logger.log("find by property name failed", Level.FATAL,null, re);
			throw re;
		}
	}
	
	public List<TarjetaEntity> findByBintarjeta(Object bintarjeta) {
		return findByProperty(BINTARJETA, bintarjeta);
	}
	
	public List<TarjetaEntity> findByNumerotarjeta(Object numerotarjeta) {
		return findByProperty(NUMEROTARJETA, numerotarjeta);
	}
	
	public List<TarjetaEntity> findByTipotarjeta(Object tipotarjeta) {
		return findByProperty(TIPOTARJETA, tipotarjeta);
	}
	
	public List<TarjetaEntity> findByDescripciontarjeta(Object descripciontarjeta) {
		return findByProperty(DESCRIPCIONTARJETA, descripciontarjeta);
	}
	
	public List<TarjetaEntity> findByEntidadcontrato(Object entidadcontrato) {
		return findByProperty(ENTIDADCONTRATO, entidadcontrato);
	}
	
	public List<TarjetaEntity> findByOficinacontrato(Object oficinacontrato) {
		return findByProperty(OFICINACONTRATO, oficinacontrato);
	}
	
	public List<TarjetaEntity> findByTipocontrato(Object tipocontrato) {
		return findByProperty(TIPOCONTRATO, tipocontrato);
	}
	
	public List<TarjetaEntity> findByNumerocontrato(Object numerocontrato) {
		return findByProperty(NUMEROCONTRATO, numerocontrato);
	}
	
	public List<TarjetaEntity> findByMontolimite(Object montolimite) {
		return findByProperty(MONTOLIMITE, montolimite);
	}
	
	public List<TarjetaEntity> findByMontodispuesto(Object montodispuesto) {
		return findByProperty(MONTODISPUESTO, montodispuesto);
	}
	
	public List<TarjetaEntity> findByNumerodocumento(Object numerodocumento) {
		return findByProperty(NUMERODOCUMENTO, numerodocumento);
	}
	
	public List<TarjetaEntity> findByIndicadorsituacion(Object indicadorsituacion) {
		return findByProperty(INDICADORSITUACION, indicadorsituacion);
	}
	
	public List<TarjetaEntity> findByUsuarioregistro(Object usuarioregistro) {
		return findByProperty(USUARIOREGISTRO, usuarioregistro);
	}
	
	public List<TarjetaEntity> findByUsuariomodificacion(Object usuariomodificacion) {
		return findByProperty(USUARIOMODIFICACION, usuariomodificacion);
	}
	

	//@SuppressWarnings("unchecked")
	public List<TarjetaEntity> findAll() {
		logger.log("finding all TarjetaEntity instances", Level.INFO, null,null);
		try {
			String queryString = "select model from TarjetaEntity model";
			return getSession().createQuery(queryString).list();
		} catch (RuntimeException re) {
			logger.log("find all failed", Level.FATAL,null, re);
			throw re;
		}
	}
}